home *** CD-ROM | disk | FTP | other *** search
/ MacTech 1 to 12 / MacTech-vol-1-12.toast / Source / MacTech® Magazine / Volume 03 - 1987 / 03.11 Nov 87 / C string library / PStrLib Source / Num2PStr.c < prev    next >
Encoding:
C/C++ Source or Header  |  1987-10-21  |  1.2 KB  |  30 lines  |  [TEXT/KAHL]

  1. /*    FILE:    Num2PStr.c    
  2.     Creates a pascal string representation of a given
  3.     type of number, returning TRUE if it fails to do so. */
  4. #include    "PStrLib.h"
  5.  
  6. Num2PStr(type, numPtr, pStr, format, places)
  7. int        type;        /* type of variable pointed at bt numPtr */
  8. void            *numPtr;    /* size of *numPtr depends on type */
  9. register char    *pStr;        /* points to a PASCAL string */        
  10. int             format;     /* specifies Decimal or Scientific notation */
  11. int                places;        /* # of digits to right of Decimal point */
  12. {    
  13.     auto        Decimal        _Decimal_;
  14.     auto        DecForm        _DecForm_;
  15.     register    int            n = 1;
  16.     
  17.     /* NOTE: When Dec2Str() fails, it sets pStr == '?'. The most common
  18.             cause of failure is trying to convert a large number to
  19.             DEC string format using a large places value. The do-while
  20.             loop below catches such failures and fixes them by changing
  21.             the format to SCI which virtually never fails (See the Apple
  22.             Numerics Manual for more details).  */ 
  23.     do {
  24.         _DecForm_.style = n > 0 ? format : FLOATDECIMAL;
  25.         _DecForm_.digits = format == FLOATDECIMAL ? places + 1 : places;
  26.          fp68k(&_DecForm_, numPtr, &_Decimal_, type + FOB2D);
  27.          Dec2Str(_DecForm_, &_Decimal_, pStr);
  28.     } while (pStr[1] == '?' && --n >= 0);
  29.     return(pStr[1] == '?');        /* Returns TRUE if Dec2Str() FAILED */
  30. }